﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using MySql.Data.MySqlClient;
namespace AdministrationWeb
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private const string CONNECTION_STRING_NAME = "ApplicationConnectionString";

        protected void Page_Load(object sender, EventArgs e)
        {
            txtWarningTimeout.Focus();
            if (!IsPostBack)
            {
                ArrayList adminSetttings = new ArrayList();

                string getSettings = "select * from settings;";

                MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString);
                MySqlCommand command = new MySqlCommand(getSettings, conn);

                conn.Open();

                MySqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    int settingID = reader.GetInt32("setting_id");
                    string settingName = reader.GetString("setting_name");
                    string settingValue = reader.GetString("setting_value");
                    string settingTypeID = reader.GetString("setting_type_id");

                    switch (settingName)
                    {
                        case "EMAIL ADDRESS":
                            txtEmailAddress.Text = settingValue;
                            break;

                        case "SERVER":
                            txtServerAddress.Text = settingValue;
                            break;

                        case "PASSWORD":
                            txtPassword.Text = settingValue;
                            break;

                        case "PORT":
                            txtPort.Text = settingValue;
                            break;

                        case "WARNING TIMEOUT":
                            txtWarningTimeout.Text = settingValue;
                            break;

                        case "LOGOUT TIMEOUT":
                            txtLogoutTime.Text = settingValue;
                            break;

                        case "USERNAME":
                            txtUserName.Text = settingValue;
                            break;

                        case "REQUIRESSPA":
                            if (settingValue == "TRUE")
                            {
                                spaNo.Checked = false;
                                spaYes.Checked = true;
                            }
                            else
                            {
                                spaYes.Checked = false;
                                spaNo.Checked = true;
                            }
                            break;

                        default:
                            break;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string updateEmail = "update settings set setting_value = '" + txtEmailAddress.Text
                                                + "' where setting_name = 'EMAIL ADDRESS'";
            string updateServer = "update settings set setting_value = '" + txtServerAddress.Text
                                                + "' where setting_name = 'SERVER'";
            string updatePassword = "update settings set setting_value = '" + txtPassword.Text
                                                + "' where setting_name = 'PASSWORD'";
            string updatePort = "update settings set setting_value = '" + txtPort.Text
                                                + "' where setting_name = 'PORT'";
            string updateWarningTimeout = "update settings set setting_value = '" + txtWarningTimeout.Text
                                                + "' where setting_name = 'WARNING TIMEOUT'";
            string updateLogoutTimeout = "update settings set setting_value = '" + txtLogoutTime.Text
                                                + "' where setting_name = 'LOGOUT TIMEOUT'";           
            string updateUserName = "update settings set setting_value = '" + txtUserName.Text
                                                + "' where setting_name = 'USERNAME'";
            string updateSPA;

            if (spaYes.Checked == true)
            {
                updateSPA = "update settings set setting_value = 'TRUE' where setting_name = 'REQUIRESSPA'";
            }
            else
            {
                updateSPA = "update settings set setting_value = 'FALSE' where setting_name = 'REQUIRESSPA'";
            }

            MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString);
            MySqlCommand updateEmailCmd = new MySqlCommand(updateEmail, conn);
            MySqlCommand updateServerCmd = new MySqlCommand(updateServer, conn);
            MySqlCommand updatePasswordCmd = new MySqlCommand(updatePassword, conn);
            MySqlCommand updatePortCmd = new MySqlCommand(updatePort, conn);
            MySqlCommand updateWarningTimeoutCmd = new MySqlCommand(updateWarningTimeout, conn);
            MySqlCommand updateLogoutTimeoutCmd = new MySqlCommand(updateLogoutTimeout, conn);
            MySqlCommand updateSPACmd = new MySqlCommand(updateSPA, conn);
            MySqlCommand updateUserNameCmd = new MySqlCommand(updateUserName, conn);

            try
            {
                conn.Open();

                updateEmailCmd.ExecuteNonQuery();
                updateServerCmd.ExecuteNonQuery();
                updatePasswordCmd.ExecuteNonQuery();
                updatePortCmd.ExecuteNonQuery();
                updateWarningTimeoutCmd.ExecuteNonQuery();
                updateLogoutTimeoutCmd.ExecuteNonQuery();
                updateSPACmd.ExecuteNonQuery();
                updateUserNameCmd.ExecuteNonQuery();
                Server.Transfer("UpdateSuccess.aspx");
            }
            catch (Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Server.Transfer("default.aspx");
        }
    }
}